package c.m.d.b.beans;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

@Entity
@Table(name = "ENTITY_TYPE")
public final class EntityType implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ENTITY_TYPE_NAME", nullable = false)
    private String entityTypeName;

    //We will go with unidirectional mapping for now. Which means that the Property Type wont
    //have getEntityType. See OneToMany annotation doc for details about this functionality.
    //TODO setting fetch as eager to fix a test for now
    @OneToMany(targetEntity = PropertyType.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER,
            orphanRemoval = true)
    @JoinColumn(name = "ENTITY_TYPE_NAME")
    //TODO if this is a set then PropertyType must implement hashcode and equals or make this a list
    private Set<PropertyType> propertyTypes;

    //todo: mentioned as UTC date in the email. I guess it should be some custom type and not Date
    @Column(name = "PUBLISHED_ON", nullable = true)

    private Date publishedOn;

    public String getEntityTypeName()
    {
        return entityTypeName;
    }

    public void setEntityTypeName(String entityTypeName)
    {
        this.entityTypeName = entityTypeName;
    }

    public Set<PropertyType> getPropertyTypes()
    {
        return propertyTypes;
    }

    public void setPropertyTypes(Set<PropertyType> propertyTypes)
    {
        this.propertyTypes = propertyTypes;
    }

    public Date getPublishedOn()
    {
        return publishedOn;
    }

    public void setPublishedOn(Date publishedOn)
    {
        this.publishedOn = publishedOn;
    }

    /*
//    TODO: IS THIS REQUIRED?
    @Override
    public int hashCode()
    {
        int result = entityTypeName.hashCode();
        for (PropertyType propertyType : propertyTypes)
        {
            result = 31 * propertyType.hashCode();
        }
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return false;
        }

        if (obj == null || getClass() != obj.getClass())
        {
            return false;
        }

        EntityType that = (EntityType) obj;

        if (!entityTypeName.equals(that.getEntityTypeName()))
        {
            return false;
        }

        if (propertyTypes.equals(that.getPropertyTypes()))
        {
            return false;
        }
        return true;
    }*/
}
